package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckItem;
import com.ithema.service.CheckItemService;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 检查项管理
 *
 * @RESETCONTROLLER 包含 @Controller @ResponseBody
 * 直接让方法返回java对象,框架自动转换成json
 */

@RestController
@RequestMapping("/checkitem")
public class CheckItemController {
    @Reference  //com.alibaba.dubbo.config.annotation.Reference; 注册查找
    private CheckItemService checkItemService;


    //新增检查项
    @RequestMapping("/add")
    // @RequestBody 作用就是解析前端fromData提交过来的json数据,然后封装指定的checkItem 对象
    public Result add(@RequestBody CheckItem checkItem) {
        try {
            checkItem.getAge();
            checkItemService.add(checkItem);
            return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            //服务调用失败
            return new Result(false, MessageConstant.ADD_CHECKITEM_FAIL);
        }

    }


//    检查项分页查询//

    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = checkItemService.pageQuery(queryPageBean);
        return pageResult;
    }

    //删除检查项

    @RequestMapping("/delete")
    public Result delete(Integer id) {

        try {
            checkItemService.deleteById(id);
            return new Result(true, MessageConstant.DELETE_CHECKITEM_SUCCESS);
        } catch (Exception e) {
          e.printStackTrace();
            //删除失败
            return new Result(false, MessageConstant.DELETE_CHECKITEM_FAIL);
        }


    }


    @RequestMapping("/edit")
    // @RequestBody 作用就是解析前端fromData提交过来的json数据,然后封装指定的checkItem 对象
    public Result edit(@RequestBody CheckItem checkItem) {
        try {
            checkItemService.edit(checkItem);
            return new Result(true, MessageConstant.EDIT_CHECKITEM_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            //服务调用失败
            return new Result(false, MessageConstant.EDIT_CHECKITEM_FAIL);
        }

    }


    @RequestMapping("/findById")
    // @RequestBody 作用就是解析前端fromData提交过来的json数据,然后封装指定的checkItem 对象
    public Result findById(Integer id) {
        try {
            CheckItem checkItem = checkItemService.findById(id);
            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS,checkItem);

        } catch (Exception e) {
            e.printStackTrace();
            //服务调用失败
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
        }

    }

    @RequestMapping("/findAll")
    // @RequestBody 作用就是解析前端fromData提交过来的json数据,然后封装指定的checkItem 对象
    public Result findAll() {
        try {
            List<CheckItem> list = checkItemService.findAll();
            return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS, list);

        } catch (Exception e) {
            e.printStackTrace();
            //服务调用失败
            return new Result(false, MessageConstant.ADD_CHECKGROUP_FAIL);
        }

    }

}